package cn.tedu.store.product.webapi.controller;

import cn.tedu.store.commons.pojo.product.dto.CouponAddNewDTO;
import cn.tedu.store.commons.pojo.product.dto.CouponUpdateDTO;
import cn.tedu.store.commons.pojo.product.vo.CouponListItemVO;
import cn.tedu.store.commons.pojo.product.vo.CouponStandardVO;
import cn.tedu.store.product.service.ICouponService;
import cn.tedu.store.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "15. 优惠券管理")
@Slf4j
@Validated
@RestController
@RequestMapping("/coupons")
public class CouponController {
    @Autowired
    private ICouponService couponService;

    @ApiOperation("添加优惠券")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated CouponAddNewDTO couponAddNewDTO) {
        log.debug("开始处理[添加优惠券]的请求: 参数:{}", couponAddNewDTO);
        couponService.addNew(couponAddNewDTO);
        log.debug("添加优惠券成功!");
        return JsonResult.ok();
    }
    @ApiOperation("删除优惠券")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理[删除优惠券]的请求 参数:{}", id);
        couponService.delete(id);
        log.debug("删除优惠券成功!");
        return JsonResult.ok();
    }

    @ApiOperation("修改优惠券")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id , CouponUpdateDTO couponUpdateDTO) {
        log.debug("处理[修改优惠券]请求 参数:{},{}", id, couponUpdateDTO);
        couponService.update(id, couponUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("获取优惠券详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/get-by-id")
    public JsonResult<CouponStandardVO> getStandardById(@PathVariable Long id){
        log.debug("处理[获取优惠券详情]请求 参数:{}",id);
        CouponStandardVO result = couponService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @ApiOperation("优惠券列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<CouponListItemVO>> list() {
        log.debug("开始处理[获取优惠券列表]请求 参数:无");
        return JsonResult.ok(couponService.list());
    }
}
